﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Web.Security;
using ShopB2C.Entity;
using ShopB2C.Enums;
using ShopB2C.Helper;
using ShopB2C.Interfaces;

namespace ShopB2C.Controllers.Brower {
    public partial class BaseController : Base.BaseController {
        private static IHelp _help;
        private static IProductSearchKeys _psk;
        protected UserEntity _user;

        public BaseController() {
            _help = DataBaseExecuter.CreateClassByName<IHelp>("Help");
            _psk = DataBaseExecuter.CreateClassByName<IProductSearchKeys>("ProductSearchKeys");
            // 帮助中心
            int pageCount;
            ViewData["HelpType"] = _help.GetHelpTypeList(int.MaxValue, 1, out pageCount);
            ViewData["HelpSection"] = _help.GetHelpSectionList(0, int.MaxValue, 1, out pageCount);
        }
        protected override void Initialize(RequestContext requestContext) {
            base.Initialize(requestContext);
            // 从cookie中得到用户对象
            try {
                FormsAuthenticationTicket ticket = ((FormsIdentity)User.Identity).Ticket;
                JavaScriptSerializer json = new JavaScriptSerializer();
                _user = json.Deserialize<UserEntity>(ticket.UserData) ?? new UserEntity() { NickName = _anonymous, LevelName = _defaultRole };
            }
            catch { _user = new UserEntity() { NickName = _anonymous, LevelName = _defaultRole }; }
            ViewData["User"] = _user;
            string key = Request.QueryString["Key"];
            ViewData["SearchKey"] = !string.IsNullOrEmpty(key)
                ? key
                : ConfigHelper.GetValue(ConfigNodeNames.Search, "defaultkey");
        }
        /// <summary>
        /// 获取搜索关键字列表
        /// </summary>
        /// <param name="id">用户输入的关键字</param>
        /// <returns>JSON (ProductSearchKeysEntity)</returns>
        [HttpGet]
        public JsonResult SearchMatch(string id) {
            object skList = _psk.GetProductSearchKeysList(Server.UrlDecode(id));

            return this.Json(skList == null ? -1 : skList, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 写入登录Cookie
        /// </summary>
        /// <param name="_user">关联的用户对象</param>
        /// <param name="timeOut">过期时间</param>
        protected internal void WriteLoginCookie(UserEntity user, int timeout) {
            // 序列化对象
            JavaScriptSerializer json = new JavaScriptSerializer();
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1
                , user.Uname
                , DateTime.Now
                , DateTime.Now.AddYears(10)
                , true
                , json.Serialize(user)
                );

            string encTicket = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            if (timeout > 0)
                cookie.Expires = DateTime.Now.AddMinutes(timeout);
            if (!string.IsNullOrEmpty(FormsAuthentication.CookieDomain))
                cookie.Domain = FormsAuthentication.CookieDomain;
            Response.Cookies.Add(cookie);
        }




    }
}